import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 074. 合并区间
 * @author: WXY
 * @create: 2023-01-11 09:18
 * @Version 1.0
 **/
public class offer_twentyfive1_merge {
    public static int[][] merge(int[][] arr) {
        if (arr.length == 0) return new int[0][2];
        Arrays.sort(arr, new myComparator());
        List<int[]> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            int left = arr[i][0], right = arr[i][1];
            if (list.size() == 0 || list.get(list.size() - 1)[1] < left) {
                list.add(new int[]{left, right});
            } else {
                list.get(list.size() - 1)[1] = Math.max(right, list.get(list.size() - 1)[1]);
            }
        }
        return list.toArray(new int[list.size()][]);

    }

    public static class myComparator implements Comparator<int[]> {
        @Override
        public int compare(int[] o1, int[] o2) {
            return o1[0] - o2[0];
        }
    }
}
